from django.db import models
#用户表
class User(models.Model):
    ON = 1
    OFF = 2
    STATUS_CHOICES = {
        (ON,"正常"),
        (OFF,"锁定")
    }
    class Meta:
        verbose_name_plural = "用户列表"

    #用户名，长度为30，不允许重复
    user_name = models.CharField(max_length=30,unique=True,verbose_name="用户名")
    pwd = models.CharField(max_length=200,verbose_name="密码")#密码
    status = models.IntegerField(verbose_name="状态",choices=STATUS_CHOICES,default=ON)#用户状态，整形
    phone = models.CharField(max_length=30,unique=True,verbose_name="电话号")#电话号码
#收货地址
class Address(models.Model):
    #一对多关联，models.CASCADE级连删除
    user = models.ForeignKey('User',on_delete=models.CASCADE)
    #收货联系人
    user_name = models.CharField(max_length=20)
    #收货地址联系电话
    phone = models.CharField(max_length=30,unique=True)
    #收货地址
    address = models.CharField(max_length=200,unique=False)
    is_default = models.IntegerField()#是否默认收货地址
#商品表
class Goods(models.Model):
    K = "克"
    J = "斤"
    G = "个"
    UNIT_CHOICES = {
        (K,"克"),
        (J,"斤"),
        (G,"个")
    }
    class Meta:
        verbose_name_plural = "商品表"
    #商品名称
    good_name = models.CharField(max_length=30)
    #商品图片，需要上传
    imr_url = models.ImageField(upload_to='img')
    #商品价格
    price = models.IntegerField()
    #商品介绍
    intro = models.TextField()
    #商品和商品类型是多对多关系
    type = models.ManyToManyField('Type')
    #商品单位
    unit = models.CharField(max_length=30,choices=UNIT_CHOICES,default="克")
#商品分类
class Type(models.Model):
    class Meta:
        verbose_name_plural = "商品分类"
    #商品分类名称
    type_name = models.CharField(max_length=30,unique=True)
    img_url = models.IntegerField(urllib_to='img',default='static/images/banner01.jpg')
    def __str__(self):
        return self.type_name


#购物车
class Cart(models.Model):
    #商品数量
    num = models.IntegerField()
    # 一对多关联，models.CASCADE级连删除
    user = models.ForeignKey("User",on_delete=models.CASCADE)

    goods = models.ManyToManyField(Goods)
#订单表
class Order(models.Model):
    order_no = models.CharField(max_length=30,unique=True)
    status = models.IntegerField()
    time = models.DateTimeField(auto_now_add=True)
    user_name = models.CharField(max_length=20)
    phone = models.CharField(max_length=30)
    address = models.CharField(max_length=200)
    user = models.ForeignKey("User",on_delete=models.CASCADE)
    total_price = models.IntegerField()
    pay_status = models.IntegerField()
#订单详情表
class OrderDetial(models.Model):
    good_name = models.CharField(max_length=30)
    price = models.IntegerField()
    num = models.IntegerField()
    img_url = models.CharField(max_length=100)
    total_price = models.IntegerField()
    order = models.ForeignKey('Order',on_delete=models.CASCADE)